﻿using System;
using System.Collections.Generic;
//using System.Drawing;
using System.Linq;
using System.Text;
using Utils.SoftReferences;
using System.Xml.Serialization;

namespace GameObjects
{
    [Serializable]
    public class Hex : BaseSoftRefTarget
    {
        public int X;
        public int Y;
        public int TerrainTypeId
        {
            get { return TerrainType.Id; }
            set { _terrainTypeId = value; }
        }
        private int _terrainTypeId;

        public Hex() { }

        public Hex(SoftRefSet softRefSet, TerrainType terrainType)
            : base(softRefSet)
        {
            this.TerrainType = terrainType;
        }

        [XmlIgnore]
        public TerrainType TerrainType;

        [XmlIgnore]
        public Structure Structure;

        [XmlIgnore]
        public IEnumerable<Hex> AdjacentCells
        {
            get 
            {
                if (adjacentCells == null) throw new Exception();
                return adjacentCells; 
            }
            set { adjacentCells = value; }
        }
        [XmlIgnore]
        private IEnumerable<Hex> adjacentCells;

        public MetaData.MovementCost MovementCost
        {
            get { return TerrainType.MovementCost; }
        }

        public float SpazFactor
        {
            get { return TerrainType.SpazFactor; }
        }

        public bool Coastal
        { 
            get
            {
                return TerrainType.MovementType == MetaData.MoveType.Land && adjacentCells.Any(o => o.TerrainType.MovementType == MetaData.MoveType.Water);
            }
        }

        public int StackLimit
        {
            get
            {
                if (Structure != null)
                    return stackLimit + 1;
                return stackLimit;
            }
        }

        private int stackLimit
        {
            get { return TerrainType.StackLimit; }
        }        
    }
}
